package dyy.zc.campuspay.ui.adapter;

import android.content.Context;
import android.support.v4.view.LayoutInflaterFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import dyy.zc.campuspay.R;
import dyy.zc.campuspay.application.MyApplication;
import dyy.zc.campuspay.bean.Account;
import dyy.zc.campuspay.bean.OrderInfo;
import dyy.zc.campuspay.listener.OnItemClickListener;
import dyy.zc.campuspay.ui.activity.ZhuanZhangActivity;
import dyy.zc.campuspay.utils.ImageLoaderUtils;
import dyy.zc.campuspay.utils.Utils;
import dyy.zc.campuspay.views.CircleImageView;

/**
 * Created by yyuand on 2017.3.14.
 */

public class QueryAdapter extends RecyclerView.Adapter<QueryAdapter.MyViewHolder>{

    private List<OrderInfo> list;
    private Context context;
    private Account account;
    private OnItemClickListener onItemClickListener;
    public QueryAdapter(Context context,List<OrderInfo> list){
        this.list = list;
        this.context = context;
        account = MyApplication.getInstance().readLoginUser();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public  void addList(List<OrderInfo> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(
                LayoutInflater.from(context).inflate(R.layout.item_bill,null),onItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ImageLoaderUtils.initImage(context,
                account.getHead().getFileUrl(), holder.head, R.mipmap.ic_launcher);
        holder.name.setText(account.getNikeName());
        holder.date.setText(list.get(position).getCreatedAt());
//        holder.type.setText(list.get(position).getDescribe());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnItemClickListener onItemClickListener;
        private CircleImageView head;
        private TextView name,date;
        public MyViewHolder(View itemView,OnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
            name = Utils.findViewsById(itemView,R.id.tv_name);
            head = Utils.findViewsById(itemView,R.id.civ_head);
//            type = Utils.findViewsById(itemView,R.id.tv_type);
            date = Utils.findViewsById(itemView,R.id.tv_date);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null){
                onItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
